const db = wx.cloud.database();
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone: 0,
    page: 31,
    fixed: '',
  },



  onLoad: function (options) {
    var that = this;
    console.log(options);
    that.setData({
      area: options.area,
      geneal: options.agent
    })
    if (options.page) {
      that.setData({
        page: options.page
      })
    }
  },
  getPhoneNumber(e) {
    var that = this;
    var area = that.data.area;
    var uid = that.data.geneal;
    var page = that.data.page;

    wx.cloud.callFunction({
      name: 'getMobile',
      data: {
        weRunData: wx.cloud.CloudID(e.detail.cloudID),
      }
    }).then(res => {
      that.setData({
        mobile: res.result,
      })
      var phone = res.result
      console.log(res.result)
      wx.request({ //发送请求
        url: "https://zg99.offcn.com/index/biaodan/register/?actid=32449&callback=?",
        data: {
          phone: phone,
          area: area,
          uid: uid
        }, //接收参数
        success(res) { //返回
          var jsonStr = res.data;
          jsonStr = jsonStr.replace("(", "");
          jsonStr = jsonStr.replace(")", "");
          if (typeof jsonStr != 'object') {
            jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
            var theJson = JSON.parse(jsonStr);
          };
          console.log(theJson)
          wx.showModal({
            title: '预约成功！',
            content: '是否需要推送公告发布提醒',
            success(res) {
              if (res.confirm) {
                console.log('用户点击确定')
                wx.requestSubscribeMessage({
                  tmplIds: ['56VGRmXk6TiWyfaNSPn-ka3iXwpZil8CE3KTzTLku-U'], //报名提醒
                  success(res) {
                    console.log(res);

                    that.setData({
                      fixed: 1
                    })
                    db.collection('2022ggyy').where({
                      _openid: app.globalData.openid,
                    }).get({
                      success: res => {
                        var that = this;
                        db.collection('2022ggyy').add({
                          data: {
                            ggyy: '1',
                            phone: phone,
                          },
                          //添加成功的回调函数
                          success(res) {
                            console.log('cloud get success!')
                          }
                        });

                      },
                    })
                  },
                  fail(res) {
                    console.log(res);
                  }
                })
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })

          wx.request({
            url: 'https://tgadmin.sdwx.zgkfpt.cn/api/v1/wx_collect_info',
            method: 'POST',
            data: {
              activity_id: 8,
              mobile: that.data.mobile,
              area_code: area,
              agent: uid,
              page_index: page
            },
            success: res => {
              console.log(res);
            }
          })
        }
      })

    }).catch(err => {
      console.error(err);
    });



  },

  to19: function () {
    wx.navigateToMiniProgram({
      appId: 'wxca86930ec3e80717', //模考appid
      path: 'page/course/details?fenxiang=1&id=234279', //模考
      envVersion: 'release', //develop（开发版），trial（体验版），release（正式版） 
      success(res) {
        console.log('打开成功！')
      }
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})